"aaaa".StartsWith("aaa") returns false
Asked Answered
Q

1

18

If this is not a bug, can anyone then explain the reason behind this behavior? Indeed it seems that every odd number of letters will return false:

string test = "aaaaaaaaaaaaaaaaaaaa";
Console.WriteLine(test.StartsWith("aa"));
Console.WriteLine(test.StartsWith("aaa"));
Console.WriteLine(test.StartsWith("aaaa"));
Console.WriteLine(test.StartsWith("aaaaa"));
Console.WriteLine(test.StartsWith("aaaaaa"));
Console.WriteLine(test.StartsWith("aaaaaaa"));

yields following output when executed on a Danish system:

True
False
True
False
True
False
Questioning answered 21/3, 2013 at 12:33 Comment(8)
I don't believe it can be true. For example, in Ideone, not Can be a Culture thing maybe..Argot
I have copied and pasted your code snippet to LINQPad - every line gave true.Alphonse
How are you doing this? I copied your code and it yields all 'True'.Watt
This question is not so bad. Not everyone have the same culture!Ani
@Matthew Well surely you agree that the culture is a pretty crucial information here, and even with an appropriate culture, Mono does not reproduce this behaviour. But yeah, that was a bummer.Chitterlings
Matthew is right, I found it out myself right after I posted the question. In danish aa corresponds to "å" so that's why every odd number of a's returns false. Comparing by invariant culture fixes the problem :)Questioning
Just another Culture issue.. ideone.com/vHT1rRArgot
Sorry I removed my comment of earlier - yes, the culture is pretty important info BUT it would have been a good idea to ask about it before downvoting! The OP might not have thought about it.Denaedenarius
S
22

This is certainly due to your current culture. You may be in Danish in which aa is considered a letter. If you try changing the culture.. or the case, it shall work.

I think I remember similar behaviour with hungarian cultures and letter associations

Have a look to String StartsWith() issue with Danish text

Example:

using System;
using System.Globalization;

namespace Demo
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("da-DK");
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
            string test = "aaaaaaaaaaaaaaaaaaaa";
            Console.WriteLine(test.StartsWith("aa"));
            Console.WriteLine(test.StartsWith("aaa"));
            Console.WriteLine(test.StartsWith("aaaa"));
            Console.WriteLine(test.StartsWith("aaaaa"));
            Console.WriteLine(test.StartsWith("aaaaaa"));
            Console.WriteLine(test.StartsWith("aaaaaaa"));
        }
    }
}

This prints what the OP claims.

Sankhya answered 21/3, 2013 at 12:36 Comment(5)
Executing Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("hu-hu"); before his code still results in all lines yielding True.Kathikathiawar
My appologies.. It is danish. Try and set Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("da-DK");Sankhya
But indeed, danish yields the expected - or rather unexpected - results of the OP: Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("da");Kathikathiawar
Still doesn’t work on Mono but that may be due to lacking locale support. However, can you explain more on why this behaviour is the way it is? I don’t understand it even though I have a passable grasp of Unicode, encoding and locales.Chitterlings
Indeed, in danish "aa" corresponds to the single letter "å", which is the reason every odd number of a's resulted in false. :)Questioning

© 2022 - 2024 — McMap. All rights reserved.